﻿using System;

namespace MaoCaiJun.DataBase.Sys.Common
{
    public class Fun
    {
        #region 根据时间获得ID数
        /// <summary>
        /// 根据时间获得ID数
        /// </summary>
        /// <returns></returns>
        public static string GetIdByTime()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmssffffff_",System.Globalization.CultureInfo.CurrentCulture) + GetChar();
        }
        private static char _forGetIdByTimeByChar = (char)47;
        /// <summary>
        /// 字符自加
        /// </summary>
        /// <returns></returns>
        private static char GetChar()
        {
            if (_forGetIdByTimeByChar == 57)
            {
                _forGetIdByTimeByChar = (char)65;
            }
            else if (_forGetIdByTimeByChar == 90)
            {
                _forGetIdByTimeByChar = (char)97;
            }
            else if (_forGetIdByTimeByChar == 122)
            {
                _forGetIdByTimeByChar = (char)48;
            }
            else
            {
                _forGetIdByTimeByChar++;
            }
            return _forGetIdByTimeByChar;
        }
        #endregion 根据时间获得ID数
        /// <summary>
        /// 根据Guid获得ID数
        /// </summary>
        /// <returns></returns>
        public static string GetIdByGuid()
        {
            return Guid.NewGuid().ToString("N");
        }

        /// <summary>
        /// 格式化时间
        /// </summary>
        /// <returns></returns>
        public static string GetDateTimeFormat(System.DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
        }
        /// <summary>
        /// Md5 加密
        /// </summary>
        /// <param name="str">要加密的字符串</param>
        /// <returns>返回加过密的字符串</returns>
        public static string Md5(string str)
        {
            if (str == null) { return str; }

            byte[] data = System.Text.Encoding.Default.GetBytes(str);
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);
            return BitConverter.ToString(result);
        }
        #region 编码
        /// <summary>
        /// 编码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string SqlDecode(string str)
        {
            if (str == null) { return ""; }
            str = str.Replace("*", "$$^$$1$$^$$");//过滤SQL注入*
            str = str.Replace("_", "$$^$$2$$^$$");//过滤SQL注入_
            str = str.Replace(" ", "$$^$$3$$^$$");//过滤SQL注入空格
            str = str.Replace("\"", "$$^$$4$$^$$");//过滤SQL注入"
            str = str.Replace("'", "$$^$$5$$^$$");//过滤SQL注入'
            str = str.Replace("[", "$$^$$6$$^$$");//过滤SQL注入[
            str = str.Replace("]", "$$^$$7$$^$$");//过滤SQL注入]
            str = str.Replace("%", "$$^$$8$$^$$");//过滤SQL注入%
            str = str.Replace(":", "$$^$$9$$^$$");//过滤SQL注入:
            str = str.Replace(";", "$$^$$10$$^$$");//过滤SQL注入;
            str = str.Replace("+", "$$^$$11$$^$$");//过滤SQL注入+
            str = str.Replace("{", "$$^$$12$$^$$");//过滤SQL注入{
            str = str.Replace("}", "$$^$$13$$^$$");//过滤SQL注入}
            str = str.Replace("\n\r", "$$^$$14$$^$$");//过滤SQL注入}
            return str;
        }
        /// <summary>
        /// 解码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string SqlEncode(string str)
        {
            if (str == null) { return ""; }
            str = str.Replace("$$^$$1$$^$$", "*");//过滤SQL注入*
            str = str.Replace("$$^$$2$$^$$", "_");//过滤SQL注入_
            str = str.Replace("$$^$$3$$^$$", " ");//过滤SQL注入空格
            str = str.Replace("$$^$$4$$^$$", "\"");//过滤SQL注入"
            str = str.Replace("$$^$$5$$^$$", "'");//过滤SQL注入'
            str = str.Replace("$$^$$6$$^$$", "[");//过滤SQL注入[
            str = str.Replace("$$^$$7$$^$$", "]");//过滤SQL注入]
            str = str.Replace("$$^$$8$$^$$", "%");//过滤SQL注入%
            str = str.Replace("$$^$$9$$^$$", ":");//过滤SQL注入:
            str = str.Replace("$$^$$10$$^$$", ";");//过滤SQL注入;
            str = str.Replace("$$^$$11$$^$$", "+");//过滤SQL注入+
            str = str.Replace("$$^$$12$$^$$", "{");//过滤SQL注入{
            str = str.Replace("$$^$$13$$^$$", "}");//过滤SQL注入}
            str = str.Replace("$$^$$14$$^$$", "\n\r");//过滤SQL注入}
            return str;
        }
        #endregion 编码
        //public static bool IsUpData(string ContentType)
        //{
        //    string[] ContentTypeList ={ "text/asp", "text/aspx", "text/php" };
        //    for (int i = 0; i < ContentTypeList.Length; i++)
        //    {
        //        if (ContentType == ContentTypeList[i])
        //        {
        //            return false;
        //        }
        //    }

        //    return true;
        //}

        //public static bool IsUpData2(string Postfix)
        //{
        //    string[] postfixList ={ "asp", "aspx", "php" };
        //    for (int i = 0; i < postfixList.Length; i++)
        //    {
        //        if (Postfix == postfixList[i])
        //        {
        //            return false;
        //        }
        //    }

        //    return true;
        //}
    }
}
